<template>
  <el-card>
    <el-table
      :data="permissionList"
      border
      row-key="id"
      :expand-row-keys="['1']"
    >
      <el-table-column label="名称" prop="name" />
      <el-table-column label="权限值" prop="code" />
      <el-table-column label="跳转权限值" prop="toCode" />
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            :icon="Plus"
            size="small"
            @click="showSavePermission(row)"
            :disabled="row.type === 2"
            v-permission="'btn.Permission.add'"
          />
          <el-button
            type="warning"
            :icon="Edit"
            size="small"
            v-permission="'btn.Permission.update'"
          />
          <el-button
            type="danger"
            :icon="Delete"
            size="small"
            v-permission="'btn.Permission.remove'"
          />
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" title="添加菜单">
      <el-form
        label-width="100px"
        ref="permissionFormRef"
        :model="permissionFormData"
        :rules="permissionFormRules"
      >
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="permissionFormData.name" />
        </el-form-item>
        <el-form-item label="权限值" prop="code">
          <el-input v-model="permissionFormData.code" />
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="savePermission">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "XPermission",
};
</script>

<script lang="ts" setup>
import { ref, onMounted, reactive } from "vue";
import { Delete, Edit, Plus } from "@element-plus/icons-vue";
import { getPermissionListApi, savePermissionApi } from "@/api/acl/permission";
import type {
  PermissionList,
  PermissionItem,
} from "@/api/acl/model/permissionModel";
import { ElMessage } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";

const permissionList = ref<PermissionList>([]);

const getPermissionList = async () => {
  const res = await getPermissionListApi();
  permissionList.value = res.children;
};

onMounted(getPermissionList);

const dialogVisible = ref(false);

const permissionFormRef = ref<FormInstance>();
const permissionFormData = reactive({
  pid: "",
  level: 1,
  type: 1,
  name: "",
  code: "",
});
const permissionFormRules = reactive<FormRules>({
  name: [{ required: true, message: "请输入菜单名称", trigger: "blur" }],
  code: [{ required: true, message: "请输入菜单权限值", trigger: "blur" }],
});

const showSavePermission = (row: PermissionItem) => {
  permissionFormData.pid = row.id;
  permissionFormData.level = row.level + 1;
  permissionFormData.type = row.level + 1 === 4 ? 2 : 1;
  permissionFormData.name = "";
  permissionFormData.code = "";
  permissionFormRef.value?.clearValidate();
  dialogVisible.value = true;
};

// 添加菜单
const savePermission = async () => {
  await permissionFormRef.value?.validate();
  await savePermissionApi(permissionFormData);
  ElMessage.success("添加菜单成功");
  dialogVisible.value = false;
  getPermissionList();
};
</script>

<style scoped></style>
